#include "SkeletonAnimationManager.h"
#include "FileReader.h"
#include "SkeletonAnimationReader.h"

SkeletonAnimationManager::SkeletonAnimationManager()
{
	keyedSkeletonAnimationList = new KeyedList<SkeletonAnimation>();
	currentSkeletonAnimation = NULL;
}
SkeletonAnimationManager::SkeletonAnimationManager(KeyedList<SkeletonAnimation> * keyedSkeletonAnimationList,
							 					   SkeletonAnimation * currentSkeletonAnimation)
{
	this->keyedSkeletonAnimationList = keyedSkeletonAnimationList;
	this->currentSkeletonAnimation = currentSkeletonAnimation;
}
SkeletonAnimationManager::~SkeletonAnimationManager()
{
	if(keyedSkeletonAnimationList)
	{
		delete(keyedSkeletonAnimationList);
		keyedSkeletonAnimationList = NULL;
	}
}
bool SkeletonAnimationManager::SetAnimation(Bone * head, char * animationName)
{
	SkeletonAnimation * fetch = NULL;
	if(head && (fetch = keyedSkeletonAnimationList->GetValue(animationName)))
	{
		if(currentSkeletonAnimation != fetch)
		{
			if(currentSkeletonAnimation)
				currentSkeletonAnimation->Reset();
				
			currentSkeletonAnimation = fetch;
			currentSkeletonAnimation->SetAnimation(head);
		}
		return true;
	}
	return false;
}
void SkeletonAnimationManager::SetNextPose(Bone * head)
{
	if(currentSkeletonAnimation)
		currentSkeletonAnimation->SetNextPose(head);
}
bool SkeletonAnimationManager::LoadFromFile(char * fileName)
{
	char * name = NULL;
	SkeletonAnimation * animation = SkeletonAnimationReader::LoadFromFile(fileName, &name);
	if(name && animation)
	{
		Add(name, animation);
		return true;
	}
	return false;
}
void SkeletonAnimationManager::Add(char * key, SkeletonAnimation * animation)
{
	keyedSkeletonAnimationList->Add(key, animation);
}